// pages/refund/refund.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num:1,
    goods: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var oid = options.oid;
    var sku_code = options.sku_code;
    this.setData({
      oid: oid,
      sku_code: sku_code
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  onShow: function () {
    this.orderDetails();
  },
  orderDetails: function () {
    var that = this;
    var oid = this.data.oid;
    var sku_code = this.data.sku_code;
    api.POST({
      params: { "order_id": oid, 'sku_code': sku_code},
      url: app.globalData.base_url + 'aftersale/getGoodsInfo',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var productDetails = res.data.data;
          var total = (productDetails.number * productDetails.price).toFixed(2);
          var returnPrice = (productDetails.price * that.data.num).toFixed(2);
          that.setData({
            productDetails: productDetails,
            total: total,
            returnPrice: returnPrice
          })
        }
      }
    })
  },
  addNum:function(){
    var maxNum = this.data.productDetails.number;
    var num = this.data.num;
    var price = this.data.productDetails.price;
    if (num < maxNum){
      num++
    }
    var returnPrice = (price * num).toFixed(2)
    this.setData({
      num: num,
      returnPrice: returnPrice
    })
  },
  minusNum: function () {
    var num = this.data.num;
    var price = this.data.productDetails.price;
    if (num >1) {
      num--
    }
    var returnPrice = (price * num).toFixed(2)
    this.setData({
      num: num,
      returnPrice: returnPrice
    })
  },

  //添加问题图片
  editGoodsImgEvent: function () {
    var that = this;
    wx.chooseImage({
      count: 9 - that.data.goods.length,
      sizeType: ['compressed'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;

        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: app.globalData.base_url +"img/saveImage",
            filePath: tempFilePaths[i],
            name: 'file',
            success: function (res) {
              var data = JSON.parse(res.data);
              that.data.goods.push(data.data.url);
              that.setData({
                goods: that.data.goods
              });
            }
          })
        }
      },
    })
  },
//删除问题图片
  closeImages:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var goods = this.data.goods;
    goods.splice(index, 1);
    this.setData({
      goods: goods
    })
  },
  description:function(e){
    var value = e.detail.value
    this.setData({
      description: value
    })
  },


  //提交申请
  submit:function(){
    var order_id  = this.data.oid;
    var sku_code = this.data.sku_code;
    var munber = this.data.num;
    var describe = this.data.description;
    var img = this.data.goods;
    if (describe == "" || describe==undefined){
      util.showError("请输入问题描述~");
      return;
    }
    if (img.length<1) {
      util.showError("请上传图片~");
      return;
    }
    var params = new Object()
    var that = this;
    params.order_id = order_id;
    params.sku_code = sku_code;
    params.number = munber;
    params.describe = describe;
    params.img =img;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'aftersale/apply',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          wx.redirectTo({
            url: '../refundSecond/refundSecond?id=' + res.data.data.id
          })
        }

      },
      fail: function (fail) {
        util.showError("../../../", "获取失败，请检查网络");
      }
    })

  }

})